import BrukerMRI as bruker
import pylab as pl
import plot
#Python method of extracting raw Bruker Files
import BrukerMRI as bruker
from jupyterthemes import jtplot
# currently installed theme will be used to
# set plot style if no arguments provided
jtplot.style(theme='onedork')
jtplot.style(ticks=True, grid=False, figsize=(6, 4.5))
# Modules
from mpl_toolkits.axes_grid1 import ImageGrid
import ipywidgets as widgets
from ipywidgets import interact, interactive_output, HBox
from mpl_toolkits.axes_grid1 import make_axes_locatable
import pandas as pd
import pydicom as dcm
import math
import numpy as np
import matplotlib.pyplot as plt
import glob
from os.path import expanduser
import os
from matplotlib import cm
import cmath
# copy and paste entire directory but replace "\" with "/"
# Place a "/" at the end of the line
MainDir = "C:/Users/daniala/Desktop/mri/20191210_104049_MRMD_phantom_practice_1_3/"
ExpNum = 8
Experiment = bruker.ReadExperiment(MainDir, ExpNum)
acqp = Experiment.acqp
method = Experiment.method
raw_fid = Experiment.raw_fid
k_space_gen = Experiment.GenerateKspace()
k_space = Experiment.k_data
k_sp = bruker.phase_shift(120,0,k_space)[:,:,0]
def k_plots(k_spaces):
key_list = list(k_spaces)
Nr = len(list(k_spaces))
Nc = 2
fig = plt.figure(figsize = (30,20))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(Nr, Nc), # creates 2x3 grid of axes
axes_pad=0.8, # pad between axes in inch.
share_all=False,
cbar_location="right",
cbar_mode="each",
cbar_size="7%",
cbar_pad="5%",
)
for key,value in k_spaces.items():
c = key_list.index(key)
im = grid[2*c].imshow(np.log(1+np.abs(value[:,:])))
grid[2*c].set_xticks([])
grid[2*c].set_yticks([])
grid[2*c].set_title('k-space of ' + key)
cb = grid.cbar_axes[2*c].colorbar(im)
im2 = grid[1+2*c].imshow(abs(plot.fft_image(value[:,:])))
grid[1+2*c].set_xticks([])
grid[1+2*c].set_yticks([])
grid[1+2*c].set_title('Image from ' + key)
cb = grid.cbar_axes[1+2*c].colorbar(im2)
def phase_ramp_1(del_x,del_y,x,y,k_sp):
shape = k_sp.shape
N_x = shape[0]
N_y = shape[1]
p_ramp = np.ones((N_x,N_y),dtype='complex')
ky = (np.linspace(0,N_y-1,N_y)-(N_y/2))/N_y
kx = (np.linspace(0,N_x-1,N_x)-(N_x/2))/N_x
if x > 0:
for i in range(0,N_x-1,x):
p_ramp[i,:] = np.exp(-1j*del_x*kx*2*np.pi)
if y > 0:
for i in range(0,N_y-1,y):
p_ramp[:,i] = p_ramp[:,i] * np.exp(-1j*del_y*ky*2*np.pi)
return(p_ramp)
first_x = phase_ramp_1(5,0,2,0,k_sp)
first_y = phase_ramp_1(0,5,0,2,k_sp)
first_xy = phase_ramp_1(5,5,2,2,k_sp)
first_x1 = phase_ramp_1(5,0,2,0,k_sp[0:30,0:30])
first_y1 = phase_ramp_1(0,5,0,2,k_sp[0:30,0:30])
first_xy1 = phase_ramp_1(5,5,2,2,k_sp[0:30,0:30])
k_spaces = {#'Original k-space':k_sp,
'Phase Shift X':first_x1,
'Phase Shift Y': first_y1,
'Phase Shift XY': first_xy1
}
k_plots(k_spaces)
k_first_x = np.multiply(k_sp,first_x)
k_first_y = np.multiply(k_sp,first_y)
k_first_xy = np.multiply(k_sp,first_xy)
k_spaces2 = {'Original k-space':k_sp,
'Original with Ramp X': k_first_x,
'Original with Ramp Y': k_first_y,
'Original with Ramp XY': k_first_xy
}
k_plots(k_spaces2)
from scipy import signal
image_first_x = signal.convolve2d(plot.fft_image(k_sp),plot.fft_image(first_x), boundary='symm', mode='same')
image_first_y = signal.convolve2d(plot.fft_image(k_sp),plot.fft_image(first_y), boundary='symm', mode='same')
image_first_xy = signal.convolve2d(plot.fft_image(k_sp),plot.fft_image(first_xy), boundary='symm', mode='same')
k_spaces1 = {'Original k-space':plot.fft_image(k_sp),
'Convolution of Original Image with Phase Ramp X': image_first_x,
'Convolution of Original Image with Phase Ramp Y': image_first_y,
'Convolution of Original Image with Phase Ramp XY': image_first_xy,
}
plot.comparison_i_plots(k_spaces1)
original = range(0,256,2)
shifted = range(1,256,2)
k_first_x_original = k_first_x[original]
k_first_x_shifted = k_first_x[shifted]
k_first_y_original = k_first_y[:,original]
k_first_y_shifted = k_first_y[:,shifted]
img_1 = np.fft.fftshift(np.fft.fft2(k_first_x_original))
img_2 = np.fft.fftshift(np.fft.fft2(k_first_x_shifted))
img_3 = np.fft.fftshift(np.fft.fft2(k_first_y_original))
img_4 = np.fft.fftshift(np.fft.fft2(k_first_y_shifted))
plt.figure(figsize=(15,10))
plt.subplot(321)
plt.imshow(abs(img_1),cmap='PiYG')
plt.subplot(323)
plt.imshow(abs(img_2),cmap ='PiYG')
plt.subplot(325)
plt.imshow(abs(img_2),cmap ='PiYG')
plt.imshow(abs(img_1),cmap ='PiYG',alpha = 0.7)
plt.subplot(322)
plt.imshow(abs(img_3),cmap='plasma')
plt.subplot(324)
plt.imshow(abs(img_4),cmap ='plasma')
plt.subplot(326)
plt.imshow(abs(img_4),cmap ='plasma')
plt.imshow(abs(img_3),cmap ='plasma',alpha = 0.7)
k_x = np.zeros((256,256),dtype='complex')
k_x[original] = k_first_x_original
img_1_pad = np.fft.fftshift(np.fft.fft2(k_x))
k_x_shift = np.zeros((256,256),dtype='complex')
k_x_shift[shifted] = k_first_x_shifted
img_2_pad = np.fft.fftshift(np.fft.fft2(k_x_shift))
k_y = np.zeros((256,256),dtype='complex')
k_y[:,original] = k_first_y_original
img_3_pad = np.fft.fftshift(np.fft.fft2(k_y))
k_y_shift = np.zeros((256,256),dtype='complex')
k_y_shift[:,shifted] = k_first_y_shifted
img_4_pad = np.fft.fftshift(np.fft.fft2(k_y_shift))
plt.figure(figsize=(30,20))
plt.subplot(321)
plt.imshow(abs(img_1_pad),cmap='PiYG')
plt.subplot(323)
plt.imshow(abs(img_2_pad),cmap ='PiYG')
plt.subplot(325)
plt.imshow(abs(img_2_pad),cmap ='PiYG')
plt.imshow(abs(img_1_pad),cmap ='PiYG',alpha = 0.7)
plt.subplot(322)
plt.imshow(abs(img_3_pad),cmap='plasma')
plt.subplot(324)
plt.imshow(abs(img_4_pad),cmap ='plasma')
plt.subplot(326)
plt.imshow(abs(img_4_pad),cmap ='plasma')
plt.imshow(abs(img_3_pad),cmap ='plasma',alpha = 0.7)